" --------------------------------------------------------------
" Custon key map
" --------------------------------------------------------------
imap fj    <ESC>
nmap L     $
nmap H     ^

nmap fw    :w<CR>
nmap fq    :q<CR>
nmap fwq   :wq<CR>

" 分屏窗口移动 Smart way to move between windows
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-h> <C-w>h
nmap <C-l> <C-w>l

nmap <C-x> <C-w>x
nmap <C-c> <C-w>c
nmap <C-m> <C-w>p

" remap U to <c-r> for easier redo
map U  <C-r>

" AutoCommands
au BufRead,BufNewFile {*.go}                   setl ft=go
au BufRead,BufNewFile {*.md,*.mkd,*.markdown}  setl ft=markdown

" au BufNewFile * normal g

" Function disable or enable number
function! HideNumber()
    if(&relativenumber == &number)
        set relativenumber! number!
    elseif(&number)
        set number!
    else
        set relativenumber!
    endif
    set number?
endfunction

" for # indent, python 文件中输入新行时#号注释不切回行首
autocmd BufNewFile,BufRead *.py inoremap # X<c-h>#

" --------------------------------------------------------------
" hdima/python-syntax
" --------------------------------------------------------------
if has('python') || has('python3')
    let g:python_highlight_all = 1
end

" --------------------------------------------------------------
" mattn/emmet-vim
" --------------------------------------------------------------
let g:user_emmet_mode='n'    "only enable normal mode functions.
" let g:user_emmet_mode='inv'  "enable all functions, which is equal to
" let g:user_emmet_mode='a'    "enable all function in all mode.
let g:user_emmet_settings = {
      \  'php':{
      \    'extends':'html',
      \    'filters':'c',
      \  },
      \  'xml':{
      \    'extends':'html',
      \  },
      \  'haml':{
      \    'extends':'html'
      \  },
      \}

" redefine trigger key
let g:user_emmet_leader_key = '<C-y>'

" Enable just for html/css
let g:user_emmet_install_global = 0
autocmd FileType html,css EmmetInstall


" --------------------------------------------------------------
" a.vim
" --------------------------------------------------------------
map <F1> :A<CR>

" --------------------------------------------------------------
" cpp.vim
" --------------------------------------------------------------

" --------------------------------------------------------------
" scrooloose/nerdcommenter
" --------------------------------------------------------------
" 我主要用于C/C++代码注释(其它的也行)
" 以下为插件默认快捷键，其中的说明是以C/C++为例的，其它语言类似
" <Leader>ci 以每行一个 /* */ 注释选中行(选中区域所在行)，再输入则取消注释
" <Leader>cm 以一个 /* */ 注释选中行(选中区域所在行)，再输入则称重复注释
" <Leader>cc 注释选中行或区域，再输入则称重复注释
" <Leader>cu 取消选中区域(行)的注释，选中区域(行)内至少有一个 /* */
" <Leader>ca 在/*...*/与//这两种注释方式中切换（其它语言可能不一样了）
" <Leader>cA 行尾注释
let NERDSpaceDelims = 1 "在左注释符之后，右注释符之前留有空格

" --------------------------------------------------------------
" scrooloose/nerdcommenter
" --------------------------------------------------------------
"

" --------------------------------------------------------------
" octol/vim-cpp-enhanced-highlight 
" --------------------------------------------------------------
" C++ 语言颜色与高亮
let g:cpp_class_scope_highlight = 1
let g:cpp_member_variable_highlight = 1
let g:cpp_class_decl_highlight = 1
let g:cpp_concepts_highlight = 1

" --------------------------------------------------------------
" msanders/snipmate.vim 
" --------------------------------------------------------------
" 代码片段模板插入

" --------------------------------------------------------------
" gmarik/vim-markdown 
" --------------------------------------------------------------
autocmd BufNewFile,BufRead *.md set filetype=markdown
let g:vim_markdown_folding_disabled=1       "折叠
let g:vim_markdown_frontmatter=1
let g:vim_markdown_conceal = 0
" 打开文本目录
nnoremap <Leader>to :Toc <CR>
" 格式化表格
nnoremap <Leader>tf :TableFormat <CR>

" --------------------------------------------------------------
" godlygeek/tabular 
" --------------------------------------------------------------

" --------------------------------------------------------------
" majutsushi/tagbar 
" --------------------------------------------------------------
" 列出当前文件中的所有宏,全局变量, 函数名等
" :h tagbar.txt
" 常规模式下输入 tb 调用插件，如果有打开 TagList 窗口则先将其关闭
" nnoremap <F4> :TlistClose<CR>:TagbarToggle<CR>
let g:tagbar_width=45                       "设置窗口宽度
let g:tagbar_left=1                         "在左侧窗口中显示

" --------------------------------------------------------------
" winmanager 
" --------------------------------------------------------------
let g:winManagerWidth=30
let g:winManagerWindowLayout='FileExplorer|TagList'
nnoremap <F4> :WMToggle<cr>

" --------------------------------------------------------------
" taglist.vim 
" --------------------------------------------------------------
" 列出当前文件中的所有宏,全局变量, 函数名等
" :h taglist.txt
" 常规模式下输入 tl 调用插件，如果有打开 Tagbar 窗口则先将其关闭
nnoremap tl :TagbarClose<CR>:Tlist<CR>
let Tlist_Show_One_File=1                   "只显示当前文件的tags
" let Tlist_Enable_Fold_Column=0
"使taglist插件不显示左边的折叠行
let Tlist_Exit_OnlyWindow=1
"如果Taglist窗口是最后一个窗口则退出Vim
let Tlist_File_Fold_Auto_Close=1            "自动折叠
let Tlist_WinWidth=50                       "设置窗口宽度
let Tlist_Use_Right_Window=1                "在右侧窗口中显示

" --------------------------------------------------------------
" DoxygenToolkit.vim 
" --------------------------------------------------------------
" 插入作者
nnoremap <Leader>da :DoxAuthor<CR>

" 插入函数/类注释
nnoremap <Leader>dx :Dox<CR>

let g:DoxygenToolkit_briefTag_funcName = "yes"
let g:DoxygenToolkit_commentType   = "C++"
let g:DoxygenToolkit_briefTag_pre  = "@Synopsis  "
let g:DoxygenToolkit_paramTag_pre  = "@Param "
let g:DoxygenToolkit_returnTag     = "@Returns   "
let g:DoxygenToolkit_blockHeader   = "//////////////////////////////////////////"
let g:DoxygenToolkit_blockFooter   = "//////////////////////////////////////////"
let g:DoxygenToolkit_classTag      = "@Class"
let g:DoxygenToolkit_blockTag      = "@Name:"
let g:DoxygenToolkit_fileTag       = "@File:"
let g:DoxygenToolkit_authorTag     = "@Author:"
let g:DoxygenToolkit_dateTag       = "@Date:"
let g:DoxygenTookit_versionTag     = "@Version:"
let g:DoxygenToolkit_authorName    = "Leon"
let g:doxygen_enhanced_color       = 1

let s:licenseTag = "Copyright(C) \<enter>"
let s:licenseTag += "For free \<enter>"
let s:licenseTag += "All right reserved\<enter>"
let g:DoxygenToolkit_licenseTag    = s:licenseTag

" --------------------------------------------------------------
" Plugin winmanager 
" --------------------------------------------------------------
let g:winManagerWidth=30
let g:winManagerWindowLayout='FileExplorer|TagList'
map <F4> :WMToggle<cr>

" --------------------------------------------------------------
" bufexplorer.zip
" --------------------------------------------------------------
let g:bufExplorerDefaultHelp=0       " Do not show default help.
let g:bufExplorerShowRelativePath=1  " Show relative paths.
let g:bufExplorerSortBy='mru'        " Sort by most recently used.
let g:bufExplorerSplitRight=1        " Split left.
let g:bufExplorerSplitVertical=1     " Split vertically.
let g:bufExplorerSplitVertSize=40    " Split width
let g:bufExplorerFindActive=1        " Open in new window.
autocmd BufWinEnter \[Buf\ List\] setl nonumber"

" --------------------------------------------------------------
" scrooloose/nerdtree 
" --------------------------------------------------------------
" 有目录村结构的文件浏览插件
"close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

let g:NERDChristmasTree=0
let g:NERDTreeSize=30
let g:NERDTreeDirArrowExpandable='+'
let g:NERDTreeDirArrowCollapsible='-'
let g:NERDTreeHidden=1
let g:NERDTreeDirArrows=1
let g:NERDTreeChDirMode=2
let g:NERDTreeQuitOnOpen=1
let g:NERDTreeKeepTreeInNewTab=1
let g:NERDTreeShowBookmarks=1
" let g:NERDTreeWinPos = "right"
let g:NERDTreeIgnore=['\~$', '\.py[cd]$', '\.swp$', '\.swo$', '^\.git$', '^\.svn$', '^\.hg$', '\.bzr$']
let g:nerdtree_tabs_open_on_gui_startup=0
nnoremap <F2> :NERDTreeToggle<CR>
nnoremap <F3> :NERDTreeCWD<CR>
nnoremap <Leader>nf :NERDTreeFind<CR>

" --------------------------------------------------------------
" scrooloose/syntastic
" --------------------------------------------------------------

" --------------------------------------------------------------
" Lokaltog/vim-easymotion
" --------------------------------------------------------------
" 快速移动插件

" --------------------------------------------------------------
" tpope/vim-surround
" --------------------------------------------------------------
" 快速给单词/句子两边增加符号（包括html标签），缺点是不能用"."来重复命令
" 不过 repeat 插件可以解决这个问题，详细帮助见 :h surround.txt
" cs    " change surrondings
" ds    " delete surrondings
" ysiw  " add surrondings between the word

"  Old text                  Command     New text ~
"  "Hello *world!"           ds"         Hello world!
"  [123+4*56]/2              cs])        (123+456)/2
"  "Look ma, I'm *HTML!"     cs"<q>      <q>Look ma, I'm HTML!</q>
"  if *x>3 {                 ysW(        if ( x>3 ) {
"  my $str = *whee!;         vllllS'     my $str = 'whee!';


" --------------------------------------------------------------
" Plugin Tagbar
" --------------------------------------------------------------
" 列出当前文件中的所有宏,全局变量, 函数名等
" :h tagbar.txt
" 常规模式下输入 tb 调用插件，如果有打开 TagList 窗口则先将其关闭
nnoremap <F3> :TlistClose<CR>:TagbarToggle<CR>
let g:tagbar_width=45                       "设置窗口宽度
" let g:tagbar_left=1                         "在左侧窗口中显示

" --------------------------------------------------------------
" Plugin undotree
" --------------------------------------------------------------
if isdirectory(expand("~/.vim/bundle/undotree"))
    nmap <Leader>ud :UndotreeToggle<CR>
    let g:undotree_SetFocusWhenToggle=1
    if has("persistent_undo")
        set undodir=~/.undodir/
        set undofile
    endif
endif

" --------------------------------------------------------------
" Plugin ag.vim
" --------------------------------------------------------------
let g:ag_working_path_mode="r"

" --------------------------------------------------------------
" Plugin delimitMate 括号自动补全
" --------------------------------------------------------------
let g:delimitMate_expand_cr = 1
" for python docsting 特别有用
au FileType python let b:delimitMate_nesting_quotes = ['"']

" 关闭某些类型的文件的自动补全，比如说关闭python文件的
"au FileType python let b:delimitMate_autoclose = 0

" --------------------------------------------------------------
" Plugin TaskList
" --------------------------------------------------------------
"  指定任务列表窗口打开位置，默认是窗口上方打开
"   0 = Open on top
"   1 = Open on the bottom
let g:tlWindowPosition = 1

"  任务列表搜索的标识符列表，默认是 'FIXME TODO XXX'.
"  搜索到得结果按照分组和出现顺序排列.
let g:tlTokenList = ["TODO", "UNDO", "FIXME"]

" --------------------------------------------------------------
" Function SetTags 
" --------------------------------------------------------------
let g:self_tags="tags"
set tags=g:self_tags

function! SetTags()
    let curdir = substitute(getcwd(), $HOME, "~", "g")
    let dir_list = split(curdir, "/")
    let work_path = "/data/ziselei/xxworkspace/server/project"
    let module_list = ["FsGame","FsPubLogic", "FsMergeLogic"]
    let basetags = g:self_tags
    let workbasetag = work_path."/define/tags".",".work_path."/public/tags".",".work_path."/server/tags".",".work_path."/utils/tags"
    let new_tags=""

    for i in range(len(dir_list))
        let dir_name = get(dir_list, i)
        if count(module_list, dir_name) > 0
            let new_tags = work_path."/".dir_name."/tags"
        endif
    endfor


    let ttags = ''.basetags.",".workbasetag
    if len(new_tags) > 0
        exe 'set tags='.ttags.",".new_tags.";"
    else
        exe 'set tags='.ttags.";"
    endif
endfunction

call SetTags()

" --------------------------------------------------------------
" VimDiff
" --------------------------------------------------------------
highlight DiffAdd cterm=bold ctermfg=lightgreen ctermbg=20 gui=none guifg=bg guibg=Red
highlight DiffDelete cterm=bold ctermfg=lightgreen ctermbg=20 gui=none guifg=bg guibg=Red
highlight DiffChange cterm=bold ctermfg=lightgreen ctermbg=20 gui=none guifg=bg guibg=Red
highlight DiffText cterm=bold ctermfg=lightgreen ctermbg=88 gui=none guifg=bg guibg=Red

""highlight! link DiffText MatchParen
highlight Pmenu    ctermbg=blue  guifg=blue
highlight PmenuSel ctermbg=lightgrey guifg=white

